const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

// 读取数组值
const transFromData = (arr, data) =>{
  let temp = arr.filter(item=>item.value==data);
  if(temp.length>0){
    return temp[0].label
  }
  return ""
}

// 拼接请求参数
const transUrlData = (obj) => {
  let arr = [];
  Object.keys(obj).forEach(function (key) {
    if (typeof obj[key] == 'object') {
      if (obj[key] == null) {
        arr.push(`${key}=`)
      } else {
        if (obj[key] == 'object') {
          obj[key] = obj[key].map(item => JSON.stringify(item)).join(',')
        }
        arr.push(`${key}=${JSON.stringify(obj[key])}`)
      }
    } else {
      arr.push(`${key}=${obj[key]}`)
    }
  });
  return '?' + arr.join('&')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

// 百度转高德地图坐标
const bdToGcj = (bdLng, bdLat) => {
  const x = bdLng - 0.0065;
  const y = bdLat - 0.006;
  const z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * Math.PI);
  const theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * Math.PI);
  const ggLng = z * Math.cos(theta);
  const ggLat = z * Math.sin(theta);
  return {
    lng: ggLng,
    lat: ggLat
  };
}

module.exports = {
  formatTime,
  transFromData,
  transUrlData,
  bdToGcj
}
